package com.qiong.algorithm;

import java.util.HashSet;
import java.util.Set;

public class SingleNum {
    public static void main(String[] args) {
        int i = SingleNum.singleNumber(new int[]{1, 2, 3, 2, 3});
        System.out.println(i);
    }

    public static int singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            Integer num = nums[i];
            if (set.contains(num)) {
                set.remove(num);
            } else {
                set.add(num);
            }
        }
        return set.size() == 1 ? set.stream().findFirst().get() : 0;
    }

    public static int singleNumberWithBetter(int[] nums) {
        int single = 0;
        for (int i = 0; i < nums.length; i++) {
            single ^= nums[i];
        }
        return single;
    }
}
